/**
 * 由 WangYu 创建
 * 日期 2017/9/25
 */
import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Image,
    TouchableOpacity, Dimensions
} from 'react-native';
import CommonNavigationBar from "../../view/CommonNavigationBar";
import {themeColor} from "../../util/Config";
import ItemView from "../../view/ItemView";
import DashedLine from "../../view/DashedLine";
import NetUtils from "../../util/NetUtils";
import Api from "../../util/Api";
import {Toast} from "teaset";
import CommonUtil from "../../util/CommonUtil";
import {connect} from "react-redux";
let screenWidth = Dimensions.get('window').width;
class MinePage extends Component {

    constructor(props) {
        super(props);
        this.state = {
            maxLoanAmount: 5000.00,
            rate: 1.0,
            repayAmount: 0.00,
        }
    }

    render() {
        return (
            <View style={styles.container}>
                <CommonNavigationBar title="我的" showLeftIcon={false}/>
                <View style={{backgroundColor: '#dadada', paddingLeft: 8, paddingRight: 8}}>
                    <View style={styles.itemStyle}>
                        <Text style={{color: 'black', fontSize: 14, marginTop: 15}}>最高可获得钱包借款额度(元)</Text>
                        <Text style={{
                            color: themeColor,
                            fontSize: 30,
                            marginTop: 10
                        }}>{CommonUtil.fmoney(this.state.maxLoanAmount, 2)}</Text>
                        <Text style={{
                            color: '#a2a2a2',
                            fontSize: 10,
                            marginTop: 10,
                            marginBottom: 15
                        }}>月费率{CommonUtil.fmoney(this.state.rate, 1)}%起</Text>
                    </View>
                    <View style={styles.itemStyle}>
                        <ItemView title='立即还款'/>
                        <DashedLine width={screenWidth - 44}/>
                        <View style={{flexDirection: 'row', height: 45, alignItems: 'center'}}>
                            <Text style={{
                                color: themeColor,
                                fontSize: 14
                            }}>{CommonUtil.fmoney(this.state.repayAmount, 2)}</Text>
                            <Text
                                style={{color: '#a2a2a2', fontSize: 10, marginLeft: 10, marginTop: 3}}>30天内待还款金额</Text>
                        </View>
                    </View>
                    <View style={styles.itemStyle}>
                        <ItemView title='还款记录'/>
                        <DashedLine width={screenWidth - 44}/>
                        <ItemView title='借款记录'/>
                    </View>
                    <View style={styles.itemStyle}>
                        <ItemView title='申请开户'/>
                        <DashedLine width={screenWidth - 44}/>
                        <ItemView title='帮助中心'/>
                    </View>
                </View>
            </View>
        );
    }

    shouldComponentUpdate(nextProps, nextState) {
        if (nextProps.isLogin !== this.props.isLogin) {
            //登录状态发生变化
            this.getData()
            // return true;
        }
        return true;
    }

    componentDidMount() {
        this.getData();
    }

    getData() {
        if (Api.isLogin) {
            NetUtils.post(Api.mine, null, result => {
                if (result.code === 1) {
                    this.setState({
                        maxLoanAmount: result.object.maxLoanAmount,
                        rate: result.object.monthRate,
                        repayAmount: result.object.repayAmount
                    });
                }
            })
        }
    }
}
const mapStateToProps = store => ({
    isLogin: store.login.isLogin,
});
export default connect(mapStateToProps)(MinePage);
const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#dadada'
    },
    itemStyle: {
        backgroundColor: 'white',
        borderRadius: 4,
        borderWidth: 0.5,
        borderColor: '#aaaaaa',
        paddingLeft: 15,
        paddingRight: 15,
        marginTop: 8,
    }
});
